/**
 * @file test4.cpp
 * @author Xin Yuan
 * 
 * Modified by: Alejandro Cabrera
 * - Changed variable naming scheme and variable names.
 * - main() adheres to C standard
 * - Removed unnecessary header files
 *
 * Runs a series of tests on a small memory manager.
 */
#include <iostream>
#include "../include/mm.h"

int main() 
{
  using std::cout;

  MemoryManager m(72,60);
  char *p[10];
  int *ii;
  
  std::cout << "at the beginning\n";
  m.printUseList();

  // step 1
  p[0] = (char *) m.mymalloc(sizeof(char));
  if (p[0] != NULL){
    *p[0] = 'a';
  }
  else{ 
    cout << "P[0] allocation failed.\n";
  }

  cout << "after p[0] = (char *)m.mymalloc(1);\n";
  m.printUseList();

  // step 2
  p[1] = (char *) m.mymalloc(sizeof(char) * 2);
  if (p[1] != NULL) {
    p[1][0] = 'b';
    p[1][1] = 'b';
  } 
  else {
    std::cout << "P[1] allocation failed.\n";
  }

  cout << "after   p[1] = (char *)m.mymalloc(2);\n";
  m.printUseList();

  // step 3
  ii = (int *)m.mymalloc(sizeof(int));
  if (ii != NULL){
    *ii = 0x01020304;
  }
  else{ 
    cout << "ii allocation failed.\n";
  }

  cout << "after   ii = (int *)m.mymalloc(4);\n";
  m.printUseList();

  // step 4
  p[4] = (char *)m.mymalloc(sizeof(char));
  if (p[4] == NULL){
    cout << "p[4] allocation failed.\n";
  }

  cout << "after   p[4] = (char *)m.mymalloc(1);\n";
  m.printUseList();

  // step 5
  m.myfree(ii);
  cout << "after myfree(ii)\n";
  m.printUseList();

  // step 6
  m.myfree(p[0]);
  cout << "after myfree(p[0])\n";
  m.printUseList();

  // step 7
  m.myfree(p[1]);
  cout << "after myfree(p[1])\n";
  m.printUseList();

  // step 8
  m.printMemoryStatus(100);

  return 0;
}
